package cn.hs.lyxf.service.impl;

import cn.hs.lyxf.domain.*;
import cn.hs.lyxf.exception.GlobalErrorCode;
import cn.hs.lyxf.feign.SignedOrderFeign;
import cn.hs.lyxf.feign.SystemFeigen;
import cn.hs.lyxf.feign.UserFeign;
import cn.hs.lyxf.mapper.MaintenanceMapper;
import cn.hs.lyxf.mapper.MaintenanceWorkMapper;
import cn.hs.lyxf.result.JsonResult;
import cn.hs.lyxf.service.IMaintenanceService;
import cn.hs.lyxf.utils.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzy
 * @since 2022-12-09
 */
@Service
public class MaintenanceServiceImpl extends ServiceImpl<MaintenanceMapper, Maintenance> implements IMaintenanceService {

    @Autowired
    private SignedOrderFeign signedOrderFeign;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private MaintenanceMapper maintenanceMapper;
    @Autowired
    private MaintenanceWorkMapper maintenanceWorkMapper;
    @Autowired
    private SystemFeigen systemFeigen;

    /**
     * 用户访问维保签约
     *
     * @param userId 用户ID
     * @return 检查结果
     */
    @Override
    public JsonResult userSigned(Long userId) {
        // 根据用户ID查询用户信息
        User user = userFeign.get(userId);
        // 校验用户信息是否存在
        AssertUtil.isNotNull(user, GlobalErrorCode.ERROR);
        // 根据用户ID查询用户公司信息
        UserCompany userCompany = userFeign.getByUserId(userId);
        // 校验用户公司信息是否存在
        AssertUtil.isNotNull(userCompany, GlobalErrorCode.ERROR);
        // 根据公司ID查询签约订单信息
        SignedOrder order = signedOrderFeign.findByCompanyId(user.getCompanyId());
        // 如果订单存在且状态不为0或1，则返回错误信息
        if (order != null && order.getState() != -1) {
            return JsonResult.error(GlobalErrorCode.ERROR);
        }
        // 创建新的签约订单信息
        SignedOrder signedOrder = new SignedOrder();
        signedOrder.setUserId(userId);
        signedOrder.setUserPhone(user.getPhone());
        signedOrder.setCompanyName(userCompany.getCompanyName());
        signedOrder.setCreateTime(new Date());
        signedOrder.setState(0);
        signedOrder.setCompanyId(user.getCompanyId());
        // 保存或更新签约订单信息
        return signedOrderFeign.saveOrUpdate(signedOrder);
    }

    /**
     * 分配给维保员维保任务
     */
    @Override
    public JsonResult distribution(Maintenance maintenance) {
        // 参数校验
        AssertUtil.isNotNull(maintenance, GlobalErrorCode.ERROR);
        Maintenance selectById = maintenanceMapper.selectById(maintenance.getId());
        if (selectById.getEmpId() != null && selectById.getEmpId() == 0) {
            return JsonResult.error("该维保任务已有员工执行");
        }
        Long empId = maintenance.getEmpId();
        // 判断员工级别
        Employee employee = systemFeigen.getId(empId);
        Long deptId = employee.getDeptId();
        Department department = systemFeigen.getById(deptId);
        if (!department.getName().equals("维保部")) {
            return JsonResult.error(GlobalErrorCode.ERROR);
        }
        selectById.setEmpId(empId);
        maintenanceMapper.updateById(selectById);
        // 生成维保工作表
        MaintenanceWork maintenanceWork = new MaintenanceWork();
        maintenanceWork.setEmpId(selectById.getEmpId());
        maintenanceWork.setCompanyId(selectById.getUserCompanyId());
        maintenanceWork.setCreateTime(new Date());
        maintenanceWork.setState(0);
        maintenanceWorkMapper.insert(maintenanceWork);
        return JsonResult.success();
    }
}